import allure
import requests
from common.config import *
from common.opmysql import *

#爱婴室会员接口(手机号注册会员)
class TestAiyings():
    @allure.step('会员注册')
    def test_memberregister(self,env):
        dayu_url = getconf(env, "dayu_url")
        branchId=getconf(env,"branchId")
        # openId=getconf(env,"realopen_id")
        openId = getopenId()
        sql='SELECT app_id FROM `buorg`.`wechat_branch_wechat` where branch_id="'+branchId+'"'
        appid=OperationDb(env).select_one(sql)
        print (openId)
        print (branchId)
        print (appid)
        phone = getphone()
        print(phone)
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/dayu_03member/register"
        data = {"openid":openId,"appid":appid,"phone":phone,"identifierType":"PHONE"}
        r=requests.post(url,json = data,headers=headers,params={"branchId":branchId})
        print ("注册",r.json())
        return phone

    @allure.step('会员查询')
    def test_memberInfo(self,env):
        dayu_url = getconf(env, "dayu_url")
        branchId=getconf(env,"branchId")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/dayu_03member/info"
        # phone = TestAiyings().test_memberregister(env)
        phone = 15263322222
        data = {"phone":phone}
        r=requests.post(url,json = data,headers=headers,params={"branchId":branchId})
        print ("查会员",r.json())
        return (r.json()['data']['cardCode'])

    @allure.step('通过会员唯一编号发放券码')
    def test_codeAcquire(self,env):
        dayu_url = getconf(env, "dayu_url")
        token=buslogin(env)
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/coupon/codeAcquire"
        CardCode = TestAiyings().test_memberInfo(env)
        branchId = getconf(env, "branchId")
        print ("CardCode",CardCode)
        couponId=int(getconf(env, "ayscouponId"))
        sql='SELECT app_id FROM `buorg`.`wechat_branch_wechat` where branch_id="'+branchId+'"'
        appid=OperationDb(env).select_one(sql)
        data = {
            "requestNo":"xxxx",
            "clientAppId":appid,
            "couponId":couponId,
            "memberCardCode":CardCode,
            "quantity":1,
            "publishChannel":"ACITIVITY",
            "subChannelName":"yyyyyy"}
        r=requests.post(url,json = data,headers=headers,params={"Token":token})
        print ("发券",r.json())

if __name__ == '__main__':
    # a=TestAiyings()
    # a.test_codeAcquire("test")
    command='pytest -s --env=test aiyings_api.py::TestAiyings::test_memberInfo'
    os.system(command)